﻿<?php
/**
 * 
 * 
 * @author return_wdm
 *
 */
class AnnounceController extends Controller
{
	public $url_announce;
	public $announcebehavior;
	private $user_id;
	private $user_name;
	/*
	 * 
	 */
	public function beforeAction($action)
	{
		parent::beforeAction($action);
		$this->url_announce=array(
				'list'=>'admin/announce/list',
				'add'=>'admin/anounce/add'
				);
		return true;
	}
	/*
	 * 默认公告的首页
	 */
	public function actionindex()
	{
		echo yii::app()->request->baseUrl;
		echo "basepath</br>";
		echo Yii::app()->basePath;
		echo "baseurl</br>";
		echo Yii::app()->baseUrl;
		$cs = Yii::app()->getClientScript();
		print_r($cs);
	}
	/**
	 * 添加公告信息
	 */
	public function actionadd()
	{
		$cs = Yii::app()->getClientScript();
		$baseUrl = '/statics/backend';
		$cs->registerScriptFile($baseUrl.'/js/xheditor/xheditor-1.1.14-zh-cn.min.js');
		$_REQUEST['userid']=1;
		$params=array();
		$params['userid']=isset($_REQUEST['userid'])?$_REQUEST['userid']:null;
		$params['username']=isset($_REQUEST['username'])?$_REQUEST['username']:null;
		$params['subject']=isset($_REQUEST['subject'])?$_REQUEST['subject']:null;
		$params['content']=isset($_REQUEST['content'])?$_REQUEST['content']:null;
		$params['sort']=isset($_REQUEST['sort'])?$_REQUEST['sort']:null;
		$params['link']=isset($_REQUEST['link'])?$_REQUEST['link']:null;
		if(!empty($params['subject'])&&!empty($params['content'])&&!empty($params['userid']))
		{
			$this->announcebehavior = new AnnounceBehaviors();	
			$flag=$this->announcebehavior->create($params);
			if($flag==true)
			{
				$this->redirect('list');
			}
			else
			{
				$this->redirect('add');
			}
		}
		else 
			$this->render('add');
		
		
	}
	public function actionok()
	{
		$this->render('ok');
	}
	public function actionerror()
	{
		$this->render('error');
	}
	/*
	 * 按页返回信息
	 */
	public function actionList()
	{
		
		$pages=Yii::app()->request->getQuery('pages');
		$keyword=Yii::app()->request->getQuery('keyword');

		$params=null;
		$criteria=new CDbCriteria();
		if(empty($pages))
			$pages=1;
		if(!empty($keyword))
		{
			$params['keyword']=$keyword;
			$criteria->addSearchCondition('subject', $keyword,true,'OR');
			$criteria->addSearchCondition('content', $keyword,true,'OR');
		}
		$this->announcebehavior=new AnnounceBehaviors();
		$maxpages=$this->announcebehavior->getCount($criteria)/10;
		$maxpages=($maxpages>floor($maxpages))?(floor($maxpages+1)):$maxpages;
		$result=$this->announcebehavior->getList($params,$pages);
		$this->render('list',array('list'=>$result,'pages'=>$pages,'maxpages'=>$maxpages));
	}
	public function actionSearch()
	{
		$content=Yii::app()->request->getQuery('keyword');
		if(empty($content))
			$this->redirect('list');
		else
		{
			
		}
	}
	public function actionDetails()
	{
		
	}
	public function actiontestEdit()
	{
		$id=Yii::app()->request->getParam('id');
		$type=Yii::app()->request->getParam('type');
	
		$params=array();
		$params['id']=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$params['content']=isset($_REQUEST['content'])? $_REQUEST['content']:null;
		$params['subject']=isset($_REQUEST['subject'])? $_REQUEST['subject']:null;
		$params['sort']=isset($_REQUEST['sort'])?$_REQUEST['sort']:null;
	
		$params['link']=isset($_REQUEST['link'])?$_REQUEST['link']:null;
		if($type=='edit')
		{
			$model=new AnnounceBehaviors();
			$result=$model->getDetail($params['id']);
			$this->render('newedit',array('result'=>$result));
				
		}
		else
		{
			$model=new AnnounceBehaviors();
			$model->update($params);
			$this->redirect(array('announce/detail','id'=>$params['id']));
		}
	
	}
}